<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习3</title>
</head>

<body>

</body>
<script>
    //1.小九九
    for (let i = 1; i <= 9; i++) {
        let line = '';
        for (let j = 1; j <= i; j++) {
            line += `${j}*${i}=${i * j}\t`;
        }
        console.log(line);
    }

    //2.金字塔
    let height = 5;
    for (let i = 1; i <= height; i++) {
        let line = '';

        for (let j = 1; j <= height - i; j++) {
            line += ' ';
        }

        for (let k = 1; k <= 2 * i - 1; k++) {
            line += '*';
        }

        console.log(line);
    }

    //3.打印直角三角形
    let size = 5;
    for (let i = 1; i <= size; i++) {
        let line = '';
        for (let j = 1; j <= i; j++) {
            line += '*';
        }
        console.log(line);
    }

    //4.本金10000元存入银行，年利率是千分之三，每过1年，将本金和利息相加作为新的本金。计算5年后，获得的本金是多少？
    let money = 10000;
    let rate = 0.003;
    for (let year = 1; year <= 5; year++) {
        money *= (1 + rate);
    }
    console.log("5年后本金为" + money.toFixed(2));

    //5.有个人想知道，一年之内一对兔子能繁殖多少对？于是就筑了一道围墙把一对兔子关在里面。已知一对兔子每个月可以生一对小兔子，而一对兔子从出生后第3个月起每月生一对小兔子。假如一年内没有发生死亡现象，那么，一对兔子一年内（12个月）能繁殖成多少对？（兔子的规律为数列，1，1，2，3，5，8，13，21）
    function fibonacci(n) {
        if (n === 1 || n === 2) return 1;

        let prev = 1;
        let curr = 1;

        for (let i = 3; i <= n; i++) {
            let next = prev + curr;
            prev = curr;
            curr = next;
        }

        return curr;
    }

    console.log(fibonacci(12));

    //6.打印菱形
    function printLX(size) {
        for (let i = 1; i <= size; i++) {
            let line = '';

            for (let j = 1; j <= size - i; j++) {
                line += ' ';
            }

            for (let k = 1; k <= 2 * i - 1; k++) {
                line += '*';
            }
            console.log(line);
        }


        for (let i = size - 1; i >= 1; i--) {
            let line = '';

            for (let j = 1; j <= size - i; j++) {
                line += ' ';
            }

            for (let k = 1; k <= 2 * i - 1; k++) {
                line += '*';
            }
            console.log(line);
        }
    }

    printLX(5);
</script>

</html>